package com.tuyenmonkey.mkloader.type;

import com.tuyenmonkey.mkloader.model.Line;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Canvas;
import ohos.agp.utils.Point;

/**
 * Created by Tuyen Nguyen on 2/12/17.
 */

public class Radar extends LoaderView {
  private Line line;
  private float degree;



  @Override public void initializeObjects() {
    System.out.println("width : " + width + "height: " + height);
    final float size = Math.min(width, height);
    line = new Line();
    line.setPoint1(center);
    line.setPoint2(new Point(0, size / 2f));
    line.setColor(color);
    line.setWidth(5);
  }

  @Override
  public void setUpAnimation() {
    AnimatorValue fadeAnimator = new AnimatorValue();
    fadeAnimator.setLoopedCount(-1);
    fadeAnimator.setDuration(1700);
    fadeAnimator.setCurveType(Animator.CurveType.ACCELERATE);
    fadeAnimator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
      @Override
      public void onUpdate(AnimatorValue animatorValue, float v) {
        degree = (float)v * 360;
        if (invalidateListener != null) {
          invalidateListener.reDraw();
          System.out.println("reDraw()");
        }
      }
    });
    fadeAnimator.start();
  }


  @Override public void draw(Canvas canvas) {
    canvas.save();
    canvas.rotate(degree, center.getPointX(), center.getPointY());
    line.draw(canvas);
    canvas.restore();
  }
}
